package com.szml.user.mapper;

import com.szml.user.dto.ResetPasswordDTO;
import com.szml.user.dto.UserPageQueryDTO;
import com.szml.user.dto.UserUpdateDTO;
import com.szml.user.entity.User;
import com.szml.user.vo.UserInfoVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author limin
 * @date 2025-06-19 11:44
 * @description
 */
@Mapper
public interface UserMapper {
    /**
     * 查询用户名看是否存在某个用户
     * @param username
     * @return
     */
    User selectByUsername(String username);

    /**
     * 插入用户数据
     * @param user
     */
    Integer insert(User user);

    /**
     * 根据userId获取roleId
     * @param currentUserId
     * @return
     */
    @Select("select role_id from user_roles where user_id=#{currentUserId}")
    Integer selectRoleIdByUserId(Long currentUserId);

    /**
     * 查询所有用户列表（超管）
     * @return
     */
    List<UserInfoVO> selectAllUsers(UserPageQueryDTO userPageQueryDTO);


    /**
     * 普通用户只能查看自己
     * @param currentUserId
     * @return
     */
    List<UserInfoVO> selectUsersById(UserPageQueryDTO userPageQueryDTO,Long currentUserId);

    /**
     * 管理员能够查看普通用户
     * @return
     */
    List<UserInfoVO> selectNormalUsers(UserPageQueryDTO userPageQueryDTO);



    /**
     * 根据userId修改用户信息
     * @param userUpdateDTO
     * @return
     */
    int updateUserById(@Param("dto") UserUpdateDTO userUpdateDTO, Long userId);


    /**
     * 重置用户密码
     * @param resetPasswordDTO
     * @param userId
     * @return
     */
    int resetPasswordByUserId(@Param("dto")ResetPasswordDTO resetPasswordDTO, Long userId);

    /**
     * 根据userId查出User
     * @param userId
     * @return
     */
    @Select("select username,email,phone from users where user_id = #{userId}")
    User selectUserById(Long userId);

    /**
     * 根据userId查用户密码
     * @param userId
     * @return
     */
    @Select("select password from users where user_id = #{userId}")
    String selectPasswordById(Long userId);

    /**
     * 查询单个用户信息
     * @param userId
     * @return
     */
    @Select( "select user_id as userId,username, password, email, phone from users where user_id = #{userId}")
    UserInfoVO selectUserInfoById(Long userId);

    @Select("select role_code from roles where role_id = #{targetUserRoleId}")
    String selectRoleCodeByRoleId(Integer targetUserRoleId);

    @Select("select user_id as userId,username, password, email, phone, gmt_create from users where username = #{username}")
    UserInfoVO selectUserInfoByUsername(String username);

    @Insert("insert into user_roles(user_id, role_id) values(#{userId},#{roleCode})")
    void insertUserWithRoleId(Long userId,String roleCode);

    @Select("select role_id from roles where role_code = 'user' ")
    String selectRoleIdByRoleCode();

}
